package com.awt.moudle;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @authoer zsj
 * @date 2023/4/15 16:50
 */
public class FileDialogDemo {

    Frame frame = new Frame();

    FileDialog f1 = new FileDialog(frame,"选择要打开的文件",FileDialog.LOAD);
    FileDialog f2 = new FileDialog(frame,"选择要保存的路径",FileDialog.SAVE);

    Button open = new Button("打开文件");
    Button save = new Button("保存文件");


    void init(){
        open.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f1.setVisible(true);
                //获取路径以及文件
                String directory = f1.getDirectory();
                String file = f1.getFile();
                System.out.println("打开文件路径为"+directory);
                System.out.println("打开名称为"+file);
            }
        });

        save.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                f2.setVisible(true);
                String directory = f1.getDirectory();
                String file = f1.getFile();
                System.out.println("保存文件路径为"+directory);
                System.out.println("保存文件名称为"+file);
            }
        });

        frame.add(open,BorderLayout.NORTH);
        frame.add(save,BorderLayout.SOUTH);


        frame.pack();;
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        new FileDialogDemo().init();
    }
}
